package com.xiaohong.account.controller;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.account.pojo.AccountTbl;
import com.xiaohong.account.service.AccountTblService;
import com.xiaohong.account.utils.ExceptionUtil;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;

@Tag(name = "AccountTbl接口文档")
@RestController
@RequestMapping("/accountTbl")
@CrossOrigin
public class AccountTblController {

    @Autowired
    private AccountTblService accountTblService;

    /***
     * 分页条件搜索实现AccountTbl
     * @param accountTbl
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询AccountTbl",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl, @PathVariable int page, @PathVariable  int size){
        //调用AccountTblService实现分页条件查询AccountTbl
        Page pageInfo = accountTblService.findPage(accountTbl, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现AccountTbl
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询AccountTbl",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AccountTblService实现分页查询AccountTbl
        Page pageInfo = accountTblService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索AccountTbl数据
     * @param accountTbl
     * @return
     */
    @Operation(summary = "多条件查询AccountTbl",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<AccountTbl>> findList(@RequestBody(required = false) @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl){
        //调用AccountTblService实现条件查询AccountTbl
        List<AccountTbl> list = accountTblService.findList(accountTbl);
        return new Result<List<AccountTbl>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除accountTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除AccountTbl",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用AccountTblService实现根据ID删除
        accountTblService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改AccountTbl数据
     * @param accountTbl
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改AccountTbl",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl,@PathVariable Integer id){
        // 补充id
        accountTbl.setId(id);
//        int a= 1/0; // 制造错误
        //调用AccountTblService实现修改AccountTbl
        accountTblService.update(accountTbl);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增AccountTbl数据
     * @param accountTbl
     * @return
     */
    @Operation(summary = "添加AccountTbl",description = "所需权限：",security = {@SecurityRequirement(name ="Authorization3")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "AccountTbl对象",required = true) AccountTbl accountTbl){
        //调用AccountTblService实现添加AccountTbl
        accountTblService.add(accountTbl);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询AccountTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询AccountTbl",description = "所需权限：",security = {@SecurityRequirement(name ="Authorization2")} )
//    @Operation(summary = "根据ID查询AccountTbl",description = "所需权限：" )
    @Parameter(in = ParameterIn.PATH, name = "id", description = "要查询的ID", required = true)
    @GetMapping("/{id}")
    public Result<AccountTbl> findById(@PathVariable Integer id){
        //调用AccountTblService实现根据ID查询AccountTbl
        AccountTbl accountTbl = accountTblService.findById(id);
        return new Result<AccountTbl>(true,StatusCode.OK,"查询成功",accountTbl);
    }

    /***
     * 查询AccountTbl全部数据
     * @return
     */
    @Operation(summary = "查询所有AccountTbl",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")} )
    @GetMapping
    @SentinelResource(value = "findAll",
            blockHandler = "findAllHandler",blockHandlerClass = ExceptionUtil.class,
            fallback = "findAllFallback",fallbackClass = ExceptionUtil.class)
    public Result<List<AccountTbl>> findAll(){
        //调用AccountTblService实现查询所有AccountTbl
        List<AccountTbl> list = accountTblService.findAll();
//        int a =1/0;
        return new Result<List<AccountTbl>>(true, StatusCode.OK,"查询成功",list) ;
    }

//
//    // 原方法调用被限流/降级/系统保护的时候调用
//    public Result<List<AccountTbl>> findAllHandler (BlockException ex){
//        System.out.println("发送了限流/降级/系统保护");
//        ex.printStackTrace();
//        return new Result<List<AccountTbl>>(false, StatusCode.ERROR,"被降级了") ;
//    }
//
//    // 原方法调用被熔断的时候调用
//    public Result<List<AccountTbl>> findAllFallback (Throwable throwable){
//        System.out.println("发生了熔断");
//        System.out.println(throwable);
//        return new Result<List<AccountTbl>>(false, StatusCode.ERROR,"被熔断了") ;
//    }


}
